package com.authstr.login.controller;

import com.authstr.login.service.inter.LoginService;
import com.authstr.auth.jwt.ShiroJwtToken;
import com.authstr.auth.jwt.JwtUtil;
import com.authstr.auth.service.inter.RoleService;
import com.authstr.auth.service.inter.UserService;
import com.authstr.auth.shiro.ShiroUtil;
import com.authstr.basic.reqres.response.ResponseData;
import com.authstr.basic.reqres.utils.WebUtils;
import com.authstr.basic.web.controller.AbstractController;
import com.authstr.model.base.BaseUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;

@Controller
public class LoginController extends AbstractController {


    @Autowired
    LoginService loginService;

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;



    @RequestMapping("/register")
    @ResponseBody
    public ResponseData register(BaseUser model){
        return ResponseData.success("注册成功!",loginService.register(model));
    }

    @RequestMapping(value ="/login",method = RequestMethod.GET)
    public String login(ModelAndView model) {
        String token=JwtUtil.cookieAndHeaderGetToken(WebUtils.getRequest());
        if(StringUtils.hasText(token)){
            try{
                SecurityUtils.getSubject().login(new ShiroJwtToken(token));
                return REDIRECT+"pages/test/index";
            } catch (ShiroException e){
            }
        }
        return "/login";
    }

    @RequestMapping(value ="/login",method = RequestMethod.POST)
    public String loginVail(Model model){
        //验证是否已登录

        String account= WebUtils.get("account");
        String password=WebUtils.get("password");
        String remember =WebUtils.get("remember");

        BaseUser user = userService.getByAccount(account);
        String md5_password= ShiroUtil.md5(password ,user.getSalt());
        String token = JwtUtil.sign(user.getAccount(), md5_password);

        SecurityUtils.getSubject().login(new ShiroJwtToken(token));

        JwtUtil.cookieAndHeaderSetToken(WebUtils.getResponse(),token);

        model.addAttribute("token",token);

        //登录成功，记录登录日志
//        ShiroUser shiroUser = ShiroKit.getUserNotNull();

//        ShiroKit.getSession().setAttribute("sessionFlag", true);

        return REDIRECT+"pages/test/index";
    }




    @RequestMapping("/test")
    @ResponseBody
    public ResponseData register(){
        roleService.getRoleIdByUserId("1");
        return ResponseData.success(userService.getByAccount("admin"));
    }

    //退出
    @RequestMapping("/logout")
    public String logout() {
//        SecurityUtils.getSubject().logout();
//        移除Cookie中的token
        Cookie cookie = new Cookie("token","");
        cookie.setValue(null);
        cookie.setMaxAge(0);
        cookie.setPath( "/");
        WebUtils.getResponse().addCookie(cookie);
        return REDIRECT+"/login";
    }



}
